package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;

import com.cip.admin.model.User;
import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.dao.ClassYearDAO;
import com.cip.master.model.ClassYear;
import com.cip.master.service.ClassYearService;

public class ClassYearServiceImpl implements ClassYearService {

	private ClassYearDAO classYearDAO;
	private VelocityEngine velocityEngine;

	
	public String getClassYearGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<ClassYear> classYearList = getAllClassYear();
			
			List<Map<String,Object>> classYearArrayList = new ArrayList<Map<String,Object>>();
			
			for (ClassYear classYear : classYearList) {
				Map<String,Object> classYearObj = new HashMap<String, Object>();
				classYearObj.put("name", "<a href='/editClassYear?method=showEditClassYear&classYear.id="+classYear.getId()+"' style='text-decoration:underline;color:blue;'>"+classYear.getName()+"</a>");
				classYearObj.put("status", classYear.getStatus());
				
				classYearArrayList.add(classYearObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, classYearArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/** Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	private static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> classYearArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, classYearArrayList, createClassYearGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createClassYearGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Class List");
		context.put("gridId", "MY_CLASSYEARLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Class Name','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "name", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}

	
	
	public ClassYear saveOrUpdate(ClassYear classYear) {
		return getClassYearDAO().saveOrUpdate(classYear);
	}

	public List<ClassYear> getAllClassYear() throws Exception {
		return classYearDAO.getAllClassYear();
	}
	
	public ClassYear getClassYearByName(String name) throws Exception{
		return classYearDAO.getClassYearByName(name);
	}

	public ClassYear getClassYear(int id) throws Exception{
		return classYearDAO.getClassYear(id);
	}

	public void setClassYearDAO(ClassYearDAO classYearDAO) {
		this.classYearDAO = classYearDAO;
	}

	public ClassYearDAO getClassYearDAO() {
		return classYearDAO;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

}
